//
//  MainTabBar.swift
//  sinaweibo
//
//  Created by mac on 15/11/8.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
//        addSubview(composeBtn)
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    private func setupUI (){
    
        addSubview(composeBtn)
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let width = self.bounds.width / 5
        let height = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
//        for var i = 0; i < 5; i++ {
//         print(i)
////            if subviews[i] .isKindOfClass(NSClassFromString("UITabBarButton")!) 
//            if i == 2 {
//                composeBtn.frame = CGRect(x:2 * width, y: 0, width: width, height: height)
//                
//            }else {
//            subviews[i].frame = CGRectMake(CGFloat(i) * width, 0, width, height)
////                composeBtn.frame = CGRect(x: CGFloat(i) * width, y: 0, width: width, height: height)
//            }
//        }
        var index :CGFloat = 0
        for subView in subviews {
        
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            
                subView.frame = CGRectOffset(rect, width * index, 0)
                
//                index++
//                if index == 2 {
//                
//                    index += 1
//                }
                index += index == 1 ? 2 : 1
                
            }
        }
        
        composeBtn.frame = CGRectOffset(rect, 2 * width, 0)
        bringSubviewToFront(composeBtn)
        
    }
    
    //懒加载 创建 '+' 按钮
    lazy var composeBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        
        return btn
    }()

}
